package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.AdminDao;
import DAO.UserDao;
import VO.Admin;
import VO.User;

public class login extends HttpServlet {
	public login() {
		super();
	}
	public void destroy() {
		super.destroy(); 
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String idf=request.getParameter("identify");
		if(idf.equals("user")){
			User user=null;
			UserDao udao=new UserDao();
			user=udao.login(account,password);
			if(user!=null){
				System.out.print("2");
				request.getSession().setAttribute("user", user);
				response.sendRedirect("/Gym/user_index.jsp");
			//	request.getRequestDispatcher("../message.jsp").forward(request, response);
			}else{
				request.setAttribute("info","错误:用户名或密码出错");
				response.sendRedirect("/Gym/user_index.jsp");
			//	request.getRequestDispatcher("../message.jsp").forward(request, response);
			}

		}else if(idf.equals("guanliyuan")){
			Admin ads=null;
			AdminDao adao=new AdminDao();
			ads=adao.login(account,password);
			if(ads!=null){
				System.out.print("2");
				request.getSession().setAttribute("ad",ads);
				response.sendRedirect("/Gym/ad_login.jsp");
			//	request.getRequestDispatcher("../admsg.jsp").forward(request, response);
			}else{
				request.setAttribute("info", "错误:用户名或密码出错");
				response.sendRedirect("/Gym/ad_login.jsp");
			//	request.getRequestDispatcher("../admsg.jsp").forward(request, response);
			}
		}
	}
	public void init() throws ServletException {
	}
}
